<?php

declare(strict_types=1);

require_once __DIR__ . '/vendor/autoload.php';

use Symfony\Component\Ldap\Ldap;
use Symfony\Component\Ldap\Entry;
use Symfony\Component\Ldap\Adapter\QueryInterface;

// ou=users,o=5f819ffdaaf252c4df2c9266,dc=authing,dc=cn
$ldap = Ldap::create('ext_ldap', [
    'host' => 'ldap.authing.cn',
    // 'encryption' => 'ssl',
]);

$dn = 'ou=users,o=5f819ffdaaf252c4df2c9266,dc=authing,dc=cn';
$password = '06eca4ed85c807db9fc6a9d5483a4dc7';

$ldap->bind($dn, $password);

// 
$email = "shubuzuo-top@shubuzuo.top";
//
$cn = "";
//(&(objectclass=person)(ou=Maintainers))
$emailFilter = '(&(email='.$email.')(objectClass=users))';

// baseDN
$baseDN = "";

// attribute map
// left(ldap attribute) -> right(your attribute)
$configMap = [
    'mail' => 'email',
    'cn' => 'username',
    'phone' => 'mobile'
];

$query = $ldap->query($dn, $emailFilter, ['scope' => QueryInterface::SCOPE_ONE]);
$results = $query->execute()->toArray();

// 遍历结果
foreach ($results as $entry) {

    // 遍历属性
    $attributes = $entry->getAttributes();

    var_dump($attributes);
    echo 'ok';
    /* $phoneNumber = $entry->getAttribute('phoneNumber');
    $isContractor = $entry->hasAttribute('contractorCompany');
    // attribute names in getAttribute() and hasAttribute() methods are case-sensitive
    // pass FALSE as the second method argument to make them case-insensitive
    $isContractor = $entry->hasAttribute('contractorCompany', false);

    $entry->setAttribute('email', ['fabpot@symfony.com']);
    $entryManager->update($entry);

    // Adding or removing values to a multi-valued attribute is more efficient than using update()
    $entryManager->addAttributeValues($entry, 'telephoneNumber', ['+1.111.222.3333', '+1.222.333.4444']);
    $entryManager->removeAttributeValues($entry, 'telephoneNumber', ['+1.111.222.3333', '+1.222.333.4444']);

    // Removing an existing entry
    $entryManager->remove(new Entry('cn=Test User,dc=symfony,dc=com')); */
}

